package com.wiscamp.ninechapters.panels.domain.entities;

import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import cube.ddd.domain.Entity;
import lombok.Data;

import java.time.LocalDateTime;

/**
 * 权限表
 */
@Data
public class Permission implements Entity {

    /**
     * 权限编号
     */
    private long permissionId = 0L;

    /**
     * 应用编号
     */
    private long applicationId = 0L;

    /**
     * 权限代码
     */
    private String permissionCode = "";

    /**
     * 权限名称
     */
    private String permissionName = "";

    /**
     * 权限分组
     */
    private String permissionGroup = "";

    /**
     * 权限类型编号
     */
    private long permissionTypeId = 0L;

    /**
     * 权限层级
     */
    private int permissionLevel = 0;

    /**
     * 上层权限编号
     */
    private long parentId = 0L;

    /**
     * 标识符
     */
    private String identifier = "";

    /**
     * 访问类型编号
     */
    private long accessTypeId = 0L;

    /**
     * 范围类型编号
     */
    private long scopeTypeId = 0L;

    /**
     * 资源名称
     */
    private String resourceName = "";

    /**
     * 备注
     */
    private String remark = "";

    /**
     * 创建用户
     */
    private long creatorId = 0L;

    /**
     * 创建时间
     */
    @JsonDeserialize(using = LocalDateTimeDeserializer.class)
    @JsonSerialize(using = LocalDateTimeSerializer.class)
    private LocalDateTime creationTime;

    /**
     * 编辑用户
     */
    private long modifierId = 0L;

    /**
     * 编辑时间
     */
    @JsonDeserialize(using = LocalDateTimeDeserializer.class)
    @JsonSerialize(using = LocalDateTimeSerializer.class)
    private LocalDateTime modificationTime;

    /**
     * 是否启用
     */
    private boolean enabled = true;

    /**
     * 是否删除
     */
    private boolean deleted = false;

    /**
     * 权限类型名称
     */
    private String permissionTypeName = "";

    /**
     * 访问类型名称
     */
    private String accessTypeName = "";

    /**
     * 权限范围名称
     */
    private String scopeTypeName = "";

    /**
     * 创建人名称
     */
    private String creatorName = "";

    /**
     * 编辑人名称
     */
    private String modifierName = "";

    public void enable() {
        if (!this.enabled) {
            this.enabled = true;
        }
    }

    public void disable() {
        if (this.enabled) {
            this.enabled = false;
        }
    }

    public void delete() {
        if (!this.deleted) {
            this.deleted = true;
        }
    }

    public void createBy(long operatorId) {
        this.creatorId = operatorId;
        this.creationTime = LocalDateTime.now();
        this.modifierId = operatorId;
        this.modificationTime = LocalDateTime.now();
    }

    public void updateBy(long operatorId) {
        this.modifierId = operatorId;
        this.modificationTime = LocalDateTime.now();
    }
}
